<template>
  <Dialog v-model="dialogVisible" :title="dialogTitle" width="800px">
    <ContentWrap>
      <!-- 搜索工作栏 -->
      <el-form
        class="-mb-15px"
        :model="queryParams"
        ref="queryFormRef"
        :inline="true"
        label-width="68px"
      >
        <el-form-item label="" prop="keyWord">
          <el-input placeholder="请输入内容" v-model.trim="keyWord">
            <template #prepend>
              <el-select v-model="kType" placeholder="请选择" class="!w-120px">
                <el-option label="名称" value="serialServerName" />
                <el-option label="编码" value="serialServerNo" />
              </el-select>
            </template>
          </el-input>
        </el-form-item>
        <el-form-item>
          <el-button @click="handleQuery"><Icon icon="ep:search" class="mr-5px" /> 搜索</el-button>
        </el-form-item>
      </el-form>
    </ContentWrap>

    <!-- 列表 -->
    <ContentWrap>
      <el-table v-loading="loading" :data="list" :stripe="true" :show-overflow-tooltip="true">
        <el-table-column label="社区" align="center" prop="community.name" width="120" />
        <el-table-column label="集中器地址" align="center" prop="serialServerName" />
        <el-table-column label="集中器编码" align="center" prop="serialServerNo" width="120" />
        <el-table-column
          label="操作"
          align="center"
          class-name="small-padding fixed-width"
          width="120"
        >
          <template #default="scope">
            <el-button type="primary" link @click="confirmSerialServer(scope.row)">选择</el-button>
          </template>
        </el-table-column>
      </el-table>
      <Pagination
        :total="total"
        v-model:page="queryParams.pageNo"
        v-model:limit="queryParams.pageSize"
        @pagination="getList"
      />
    </ContentWrap>
  </Dialog>
</template>
<script lang="ts" setup>
import { getMeterSerialServerPage } from '@/api/zwsj/meterSerialServer'

defineOptions({ name: 'MeterSerialServerSelect' })

const emits = defineEmits(['confirmSerialServer'])

const dialogVisible = ref(false) // 弹窗的是否展示
const dialogTitle = ref('') // 弹窗的标题
const loading = ref(true) // 列表的加载中
const keyWord = ref('')
const kType = ref('serialServerName')
const total = ref(0) // 列表的总页数
const list = ref() // 列表的数据

const queryParams = reactive({
  pageNo: 1,
  pageSize: 10,
  serialServerName: '',
  serialServerNo: ''
})

/** 打开弹窗 */
const open = async () => {
  dialogVisible.value = true
  dialogTitle.value = '选择集中器'
  getList()
}
defineExpose({ open }) // 提供 open 方法，用于打开弹窗

const queryFormRef = ref() // 搜索的表单

/** 查询部门列表 */
const getList = async () => {
  loading.value = true
  try {
    if (kType.value == 'serialServerName') {
      queryParams.serialServerName = keyWord.value
    } else if (kType.value == 'serialServerNo') {
      queryParams.serialServerNo = keyWord.value
    }
    const data = await getMeterSerialServerPage(queryParams)
    list.value = data.list
    total.value = data.total
  } finally {
    loading.value = false
  }
}

/** 搜索按钮操作 */
const handleQuery = () => {
  getList()
}

/** 确认 */
const confirmSerialServer = async (row: any) => {
  emits('confirmSerialServer', row)
  dialogVisible.value = false
}

/** 初始化 **/
onMounted(async () => {
  await getList()
})
</script>
